
<template>
  <div class="container">
    <!-- 卡片面板 start-->
    <el-card>
      <!-- 面包屑 start -->
      <el-breadcrumb
        separator-class="el-icon-arrow-right"
        v-if="queryInfo.subjectID"
      >
        <el-breadcrumb-item>学科管理</el-breadcrumb-item>
        <el-breadcrumb-item>{{ queryInfo.subjectName }}</el-breadcrumb-item>
        <el-breadcrumb-item>目录管理</el-breadcrumb-item>
      </el-breadcrumb>
      <el-divider v-if="queryInfo.subjectID"></el-divider>
      <!-- 面包屑 end -->
      <!-- 增加form表单 start -->
      <el-form ref="form" :model="form" label-width="80px" class="firstform">
        <el-row :gutter="20">
          <!-- 目录名称start -->
          <el-col :span="7">
            <el-form-item label="目录名称">
              <el-input v-model="form.name"></el-input>
            </el-form-item>
          </el-col>
          <!-- 目录名称end -->
          <!-- 级联选择器 start -->
          <el-col :span="10">
            <el-form-item label="状态">
              <el-select v-model="form.region" placeholder="请选择">
                <el-option
                  :label="item.label"
                  :value="item.value"
                  :key="item.value"
                  v-for="item in form.status"
                ></el-option>
              </el-select>
              <!-- 清除按钮 -->
              <el-button class="clear" size="small" @click="removeData"
                >清除</el-button
              >
              <!-- 搜索按钮 -->
              <el-button
                class="clear"
                size="small"
                type="primary"
                @click="searchText"
                >搜索</el-button
              >
            </el-form-item>
          </el-col>
          <!-- 级联选择器 end -->
          <!-- 文字按钮 start -->

          <!-- 文字按钮 end -->
          <!-- 新增目录 start -->
          <el-col :span="7">
            <el-form-item label="" class="xingzheng">
              <el-button
                type="text"
                icon="el-icon-back"
                class="back"
                v-if="queryInfo.subjectID"
                @click="backHandle"
                >返回学科</el-button
              >
              <el-button
                size="small"
                type="success"
                icon="el-icon-edit"
                @click="dialogVisible = true"
                >新增目录</el-button
              >
            </el-form-item>
          </el-col>
          <!-- 新增目录 end -->
        </el-row>
      </el-form>
      <!-- 增加form表单 end-->
      <!--数据记录start -->
      <el-alert
        :title="alertText"
        type="info"
        :closable="false"
        show-icon
      ></el-alert>
      <!-- 数据记录 end -->
      <!-- 列表 start -->
      <el-table
        :data="dataList"
        fit
        highlight-current-row
        style="width: 100%"
        :key="tableKey"
      >
        <!-- 序号列 start-->
        <el-table-column align="center" label="序号" type="index" width="50">
        </el-table-column>
        <!-- 序号列 end -->
        <!-- 所属学科 start -->
        <el-table-column align="center" label="所属学科">
          <template slot-scope="scope">
            <span>{{ scope.row.subjectName }}</span>
          </template>
        </el-table-column>
        <!-- 所属学科 end -->
        <!-- 目录名称 start -->
        <el-table-column align="center" label="目录名称">
          <template slot-scope="scope">
            <span>{{ scope.row.directoryName }}</span>
          </template>
        </el-table-column>
        <!-- 目录名称 end -->
        <!-- 创建者start -->
        <el-table-column align="center" label="创建者">
          <template slot-scope="scope">
            <span>{{ scope.row.username }}</span>
          </template>
        </el-table-column>
        <!-- 创建者end -->
        <!-- 创建日期 start-->
        <el-table-column align="center" label="创建日期">
          <template slot-scope="scope">
            <span>{{ scope.row.addDate | parseTimeByString }}</span>
          </template>
        </el-table-column>
        <!-- 创建日期 end -->
        <!-- 面试题数量 start -->
        <el-table-column align="center" label="面试题数量">
          <template slot-scope="scope">
            <span>{{ scope.row.totals }}</span>
          </template>
        </el-table-column>
        <!-- 面试题数量 end -->
        <!-- 状态 start -->
        <el-table-column align="center" label="状态">
          <template slot-scope="scope">
            <span v-if="scope.row.state == 1">已启用</span>
            <span v-else>已禁用</span>
          </template>
        </el-table-column>
        <!-- 状态 end -->
        <!--操作按钮 start-->
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button
              type="text"
              v-if="scope.row.state == 1"
              @click="disableHandle(scope.row)"
              >禁用</el-button
            >
            <el-button type="text" @click="disableHandle(scope.row)" v-else
              >启用</el-button
            >
            <el-button
              type="text"
              @click="modifyHandle(scope.row)"
              :disabled="scope.row.state ? true : false"
              >修改</el-button
            >
            <el-button
              type="text"
              @click="deleteHandle(scope.row.id)"
              :disabled="scope.row.state ? true : false"
              >删除</el-button
            >
          </template>
        </el-table-column>
        <!--操作按钮 end -->
      </el-table>
      <!-- 列表end -->
      <!-- 分页 start -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.page"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="queryInfo.pagesize"
        layout="prev, pager, next, sizes,jumper"
        :total="total"
        background
        v-if="!queryInfo.subjectID"
      >
      </el-pagination>
      <!-- 分页 end -->
    </el-card>
    <!-- 卡片面板 end-->
    <!-- 新增目录的对话框 start -->
    <el-dialog
      title="新增目录"
      :visible.sync="dialogVisible"
      width="30%"
      @close="diagClose"
    >
      <!-- 表单start -->
      <el-form
        :model="editForm"
        :rules="addFormRules"
        ref="addFormRef"
        label-width="80px"
      >
        <el-form-item label="所属学科" v-if="!queryInfo.subjectID">
          <el-select v-model="editForm.value" placeholder="请选择">
            <el-option
              :label="item.label"
              :value="item.value"
              v-for="item in editForm.subjectList"
              :key="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- 一个表单验证框 -->
        <el-form-item label="目录名称" prop="directoryName">
          <el-input v-model="editForm.directoryName"></el-input>
        </el-form-item>
      </el-form>
      <!-- 表单end -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addDirectory">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 新增目录的对话框 end -->

    <!-- 编辑目录对话框 start -->
    <el-dialog
      title="编辑目录"
      :visible.sync="dialogVisible1"
      width="30%"
      @close="diagClose"
    >
      <!-- 表单start -->
      <el-form
        :model="editForm"
        :rules="editFormRules"
        ref="editFormRef"
        label-width="80px"
      >
        <el-form-item label="所属学科" v-if="!queryInfo.subjectID">
          <el-select v-model="editForm.value" placeholder="请选择">
            <el-option
              :label="item.label"
              :value="item.value"
              v-for="item in editForm.subjectList"
              :key="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- 一个表单验证框 -->
        <el-form-item label="目录名称" prop="directoryName" ref="b">
          <el-input v-model="editForm.directoryName"></el-input>
        </el-form-item>
      </el-form>
      <!-- 表单end -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible1 = false">取 消</el-button>
        <el-button type="primary" @click="editRequest">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 编辑目录对话框 end -->
    <!-- 删除按钮的对话框start  -->
    <!-- 删除按钮的对话框end -->
  </div>
</template>

<script>
// 导入相应的接口
import {
  list,
  changeState,
  detail,
  update,
  remove,
  add,
} from "@/api/hmmm/directorys.js";
import { simple } from "@/api/hmmm/subjects.js";
import { status } from "@/api/hmmm/constants.js";
export default {
  // 导入相应的接口
  // 组件名称
  name: "",
  // 局部注册的组件
  components: {},
  // 组件参数 接收来自父组件的数据
  // 组件状态值
  data() {
    return {
      form: {
        name: "",
        region: "",
        status: status,
      },
      dialogVisible: false,
      // 编辑目录的信息
      editForm: {
        directoryName: "",
        subjectName: "",
        // 学科的数据源
        subjectList: [],
        value: "",
      },
      // 编辑目录表单的验证规则
      editFormRules: {
        directoryName: [
          { required: true, message: "请输入目录名称", trigger: "change" },
        ],
      },
      addFormRules: {
        directoryName: [
          { required: true, message: "请输入目录名称", trigger: "change" },
        ],
      },
      // 表格数据源
      dataList: [],

      // 列表的参数的数据对象
      queryInfo: {
        page: 1,
        pagesize: 10,
        // subjectID: null,
        // directoryName: null,
        // state: null,
      },
      // 数据的总条数
      total: 0,
      // 获取学科数据参数
      reqParams: {
        subjectName: null, // 学科名
        page: null, // 页码
        pagesize: null, // 每页20条数据
      },
      // 获取学科数据

      // 记录的文字数据
      alertText: "",
      dialogVisible1: false,
      tableKey: 0,
      // 当前编辑行的id
      editId: "",
      // 增加目录的临时id
      directoryid: null,
    };
  },
  // 计算属性
  computed: {},
  // 侦听器
  watch: {},
  // 生命周期钩子   注：没用到的钩子请自行删除
  /**
   * 组件实例创建完成，属性已绑定，但DOM还未生成，$ el属性还不存在
   */
  created() {
    // 得到当前的数据源
    this.getList();
    this.getSubjectList();
    // console.log(this.$route.query
    this.queryInfo.subjectID = this.$route.query.id;
    this.editForm.value = this.$route.query.id;
    this.queryInfo.subjectName = this.$route.query.name;
  },
  /**
   * el 被新创建的 vm.el 替换，并挂载到实例上去之后调用该钩子。
   * 如果 root 实例挂载了一个文档内元素，当 mounted 被调用时 vm.el 也在文档内。
   */
  mounted() {},
  // 组件方法
  methods: {
    // 获取列表数据
    async getList() {
      const { data: res } = await list(this.queryInfo);
      this.dataList = res.items;
      this.total = res.counts;
      this.alertText = `数据一共${this.total}条`;
    },
    //获取所有学科的数据
    async getSubjectList() {
      const { data: res } = await simple();
      this.editForm.subjectList = res;
      console.log(this.editForm.subjectList);
    },
    async disableHandle(row) {
      if (row.state == 1) {
        row.state = 0;
      } else {
        row.state = 1;
      }
      try {
        await changeState({ id: row.id, state: row.state });
        this.$message.success("操作成功");
      } catch (err) {
        this.$message.error("操作失败");
        return err;
      }
    },
    deleteHandle(id) {
      this.$confirm("此操作将永久删除该目录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          await remove({ id });
          this.getList();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {});
    },
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      this.getList();
    },
    handleCurrentChange(newPage) {
      this.queryInfo.page = newPage;
      this.getList();
    },
    // 获取目录详情
    async modifyHandle(row) {
      this.dialogVisible1 = true;
      console.log(row);
      this.editForm.directoryName = row.directoryName;
      this.editForm.value = row.subjectID;
      this.editId = row.id;
    },
    // 修改目录详情
    editRequest() {
      this.$refs.editFormRef.validate(async (valid) => {
        if (!valid) return;
        await update({
          directoryName: this.editForm.directoryName,
          subjectID: this.editForm.value,
          id: this.editId,
        });
        this.getList();
        this.$message.success("操作成功");
        this.dialogVisible1 = false;
      });
    },
    // 搜索目录
    searchText() {
      if (this.form.region !== "") {
        this.queryInfo.state = this.form.region;
      }
      this.queryInfo.directoryName = this.form.name;
      this.getList();
    },
    // 清除数据
    removeData() {
      this.form.region = null;
      this.form.name = null;
    },
    // 目录的关闭事件
    diagClose() {
      //新增目录项
      if (this.$refs.addFormRef) {
        this.$refs.addFormRef.resetFields();
      } else {
        this.$refs.editFormRef.resetFields();
      }

      this.editForm.directoryName = null;
      this.editForm.value = null;
    },
    // 增加目录事件
    addDirectory() {
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) return;
        await add({
          directoryName: this.editForm.directoryName,
          subjectID: this.editForm.value,
          id: this.directoryid,
        });
        this.getList();
        this.$message.success("操作成功");

        this.dialogVisible = false;
      });
    },
    backHandle() {
      this.$router.go(-1);
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
// 使卡片有有一定的margin值
.el-card {
  margin: 10px;
}
.clear {
  margin-left: 5px;
  // 高度为34px
  height: 36px;
}
::v-deep .el-dialog__footer {
  text-align: right;
}
::v-deep .el-card__body {
  padding: 0 0 15px;
}
::v-deep .el-breadcrumb {
  margin: 20px;
}
::v-deep .firstform {
  margin: 20px;
}
::v-deep .el-table {
  margin: 0 20px;
  width: 96% !important;
}

.el-alert {
  margin-left: 20px;
  margin-right: 20px;
  margin-bottom: 15px;
  width: 96%;
}
.el-dialog .el-select {
  width: 100%;
}

.el-pagination {
  margin-top: 15px;
  margin-right: 20px;
  text-align: right;
}
.xingzheng {
  text-align: right;
  padding-right: 3px;
}
</style>
